
var registerDialogVm  = new Vue({
    el: '#appDialogRegister',
    data: {
        dialogRegisterVisible: false,
        showRefund: false,// 退款相关信息
        showSrAccountType: false, //账户类型是否展示
        showSrBankInfo: false, //银行信息是否展示
        showAlipayInfo: false, //支付宝信息是否展示
        showOrderItem: false, //显示订单明细
        showFreight: false, //显示运费相关信息
        showReceive: false, // 显示收货人相关信息
        showAddGoods: false, //显示添加商品相关信息
        showSrgStock: false,// 显示退货仓库
        serviceRegister: {}, // 售后登记注册信息
        serviceRefunds: {}, // 售后退款信息
        erpOrderInfo: {}, // erp 订单信息
        erpOrderItem: [], //erp 订单明细
        regionsSascade: [], //地区信息
        multiRrpOrderItem: [], //勾选的订单明细
        serviceReceiveStatusEnum: [],//售后收货状态枚举类
        serviceSendStatusEnum: [],// 售后发货状态枚举类
        serviceStatusEnum: [],// 售后状态枚举类
        serviceTypeEnum: [], //售后类型枚举类
        srReasonTemp: [], //退款原因临时变量
        srStockList: [],// 退货仓库列表
    },
    computed: {
        //获取对象里面的 退款路径
        srRefundPath: function () {
            return this.serviceRefunds.srRefundPath;
        },
        // 获取对象里面的 账户类型
        srAccountType: function () {
            return this.serviceRefunds.srAccountType;
        },
        // 获取对象里面的 退货仓库原因
        srOtherReason: function () {
            return this.serviceRegister.srOtherReason;
        }

    },
    watch: {
        //侦听 退款路径变化
        srRefundPath: function (val) {
            if (val == 1) { //原路退回
                this.showSrAccountType = false;
                //退款路径发生变化 账户类型则重置
                this.serviceRefunds.srAccountType = null;
            } else { // 其它
                this.showSrAccountType = true;
                //退款路径发生变化 账户类型则重置
                this.serviceRefunds.srAccountType = 1;
            }


        },
        // 侦听账户类型变化
        srAccountType: function (val) {
            if (val == 1) { // 银行卡
                this.showSrBankInfo = true;
                this.showAlipayInfo = false;

            } else if (val == 2) { // 支付宝
                this.showAlipayInfo = true;
                this.showSrBankInfo = false;
            } else {
                this.showAlipayInfo = false;
                this.showSrBankInfo = false;
            }
        },
        // 侦听退款原因变化 转换为字符串
        srReasonTemp: function (val) {
            if (val) {
                this.serviceRegister.srReason = val.join("|");
            } else {
                this.serviceRegister.srReason = null;
            }
        },
        //侦听其它退货原因变化
        srOtherReason: function (val) {
            if (val && !$.inArray("其它原因", this.srReasonTemp)) {
                this.srReasonTemp.push("其它原因");
            }
        }
    },
    methods: {
        loadData: function (data) {
            if (!data) {
                alert("页面跳转参数异常,请返回原页面");
            }
            registerDialogVm.dialogRegisterVisible = true;
            registerDialogVm.getRegisterInfo(data.orderId);
        },
        saveOrUpdate: function (event) {
            //校验数据
            if (!registerDialogVm.serviceRegister.srType) {
                this.$message.error({message: '请先选择售后类型', duration: 1000});
                return;
            }
            if (!registerDialogVm.serviceRegister.srOrderId) {
                this.$message.error({message: '售后单号不能为空', duration: 1000});
                return;
            }
            if (registerDialogVm.srReasonTemp.length == 0) {
                this.$message.error({message: '请先选择售后原因', duration: 1000});
                return;
            }

            var data = {};
            data['serviceRegister'] = registerDialogVm.serviceRegister;
            data['orderItemList'] = registerDialogVm.multiRrpOrderItem;
            data['serviceRefunds'] = registerDialogVm.serviceRefunds;
            $.ajax({
                type: "POST",
                url: baseURL + "service/serviceregister/save",
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功,跳转至售后列表页面', function (index) {
                            //registerDialogVm.reload();
                            window.parent.vm.goto({hash:'#modules/service/serviceregister.html'})
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //获取售后登记所需数据
        getRegisterInfo: function (orderId) {
            $.get(baseURL + "service/serviceregister/getRegisterInfo/" + orderId, function (r) {
                if (r.code == 500) {
                    alert(r.msg);
                    return;
                }
                // 加载订单信息
                registerDialogVm.erpOrderInfo = r.erpOrderInfo;
                registerDialogVm.erpOrderItem = r.erpOrderItemList;
                registerDialogVm.regionsSascade = JSON.parse(r.regionsSascade);
                //加载仓库
                registerDialogVm.srStockList = r.srStockList;
                //加载枚举类
                registerDialogVm.serviceTypeEnum = r.mapEnum.serviceTypeEnum;
                //初始化部分信息
                registerDialogVm.serviceRegister = r.serviceRegister;
                registerDialogVm.serviceRefunds = r.serviceRefunds;

                registerDialogVm.srTypeChange(1);

            });
        },

        //监听售后类型变化
        srTypeChange: function (value) {
            if (value == 1) { // 仅退款
                registerDialogVm.showRefund = true;
                registerDialogVm.showOrderItem = false;
                registerDialogVm.showFreight = false;
                registerDialogVm.showReceive = false;
                registerDialogVm.showAddGoods = false;
                registerDialogVm.showSrgStock = false;

            } else if (value == 2) { // 退货退款
                registerDialogVm.showRefund = true;
                registerDialogVm.showOrderItem = true;
                registerDialogVm.showFreight = true;
                registerDialogVm.showReceive = false;
                registerDialogVm.showAddGoods = false;
                registerDialogVm.showSrgStock = true;


            } else if (value == 3) { //换货
                registerDialogVm.showRefund = false;
                registerDialogVm.showOrderItem = true;
                registerDialogVm.showFreight = true;
                registerDialogVm.showReceive = true;
                registerDialogVm.showAddGoods = true;
                registerDialogVm.showSrgStock = true;


            } else if (value == 4) {// 补发
                registerDialogVm.showRefund = false;
                registerDialogVm.showOrderItem = false;
                registerDialogVm.showFreight = true;
                registerDialogVm.showReceive = true;
                registerDialogVm.showAddGoods = true;
                registerDialogVm.showSrgStock = false;

            }
        },
        //监听其它原因变化
        otherReasonChange: function (value) {
        },

        //监听订单明细勾选change事件
        orderItemSelectionChange: function (value) {
            registerDialogVm.multiRrpOrderItem = value;
        },

        //监控地区变化
        handleRegionsSascadeChange: function (value) {
            registerDialogVm.serviceRegister.srReceiveProvice = value[0];
            registerDialogVm.serviceRegister.srReceiveCity = value[1];
            registerDialogVm.serviceRegister.srReceiveArea = value[2];
        },

        //校验金额
        validAmount: function () {
            var reg = /^(([1-9]\d*)|\d)(\.\d{1,3})?$/;
            if (!reg.test(registerDialogVm.serviceRefunds.srRefundAmount)) {
                this.$message.error({message: '请输入正确的金额', duration: 1000});
                return;
            }

        },

        // 弹窗关闭事件
        closeDialogRegister: function () {
            // 清空所有加载数据
           registerDialogVm.dialogRegisterVisible = false;
           registerDialogVm.showRefund= false;// 退款相关信息
           registerDialogVm.showSrAccountType= false; //账户类型是否展示
           registerDialogVm.showSrBankInfo= false; //银行信息是否展示
           registerDialogVm.showAlipayInfo= false; //支付宝信息是否展示
           registerDialogVm.showOrderItem= false; //显示订单明细
           registerDialogVm.showFreight= false; //显示运费相关信息
           registerDialogVm.showReceive= false; // 显示收货人相关信息
           registerDialogVm.showAddGoods= false; //显示添加商品相关信息
           registerDialogVm.showSrgStock= false;// 显示退货仓库
           registerDialogVm.serviceRegister= {}; // 售后登记注册信息
           registerDialogVm.erpOrderInfo= {}; // erp 订单信息
           registerDialogVm.erpOrderItem= []; //erp 订单明细
           registerDialogVm.regionsSascade= []; //地区信息
           registerDialogVm.multiRrpOrderItem= []; //勾选的订单明细
           registerDialogVm.serviceReceiveStatusEnum= [];//售后收货状态枚举类
           registerDialogVm.serviceSendStatusEnum= [];// 售后发货状态枚举类
           registerDialogVm.serviceStatusEnum= [];// 售后状态枚举类
           registerDialogVm.serviceTypeEnum= []; //售后类型枚举类
           registerDialogVm.srReasonTemp= []; //退款原因临时变量
           registerDialogVm.srStockList= [];// 退货仓库列表

        }

    }
});